<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class CarModelObject extends Model
{

    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'car_model_object';


    public static function edit( $id, $data){

        $now = Date("Y-m-d H:i:s");
        $sql = "update car_model_object set 
                `label`=?, 
                model_id=? 
                ,`year`=? 
                ,engine_displacement=? 
                ,gearbox_lv=? 
                ,gearbox_type=? 
                ,updated_at='$now'
                where id='".$id."'";
        $res = DB::update( $sql,[
            $data['name'],
            $data['model_id'],
            $data['year'],
            $data['engine_displacement'],
            $data['gearbox_lv'],
            $data['gearbox_type']

        ] );
        // 不对 oil字段进行编辑，以作为原始记录存在
        $sql2 = "update car_model_object_info set 
                `year`=?, 
                `gearbox`=?, 
                `gearbox_lv`=?, 
                `engine`=?, 
                `oil_id`=? 
                ,`oil_change`=? 
                ,oil_regional=? 
                ,updated_at='$now'
                where mi_key='".$data['mi_key']."'";
        $res2 = DB::update( $sql2,[
            $data['year'],
            $data['gearbox_type'],
            $data['gearbox_lv'],
            $data['engine'],
            $data['oil_id'],
            $data['oil_change'],
            $data['oil_regional']
        ]);

        return $res + $res2;

    }

}